package com.iyao_app.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.flipboard.bottomsheet.commons.BottomSheetFragment;
import com.iyao_app.R;
import com.iyao_app.adapter.BaseRecyclerAdapter;
import com.iyao_app.adapter.SignSeletPictureAdapter;
import com.iyao_app.mvp.sign.SignActivity;
import com.iyao_app.util.SpaceItemDecoration;
import com.iyao_app.view.LetterSpacingTextView;
import com.iyaowoqu.base_library.util.LogUtil;
import com.lzy.imagepicker.bean.ImageItem;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/22.
 */

public class SignPictureFragment extends BottomSheetFragment {
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.tv_complete)
    LetterSpacingTextView mTvComplete;
    Unbinder unbinder;
    int pagerSize = 9;
    int pagerNum = 0;

    private SignSeletPictureAdapter adapter;
    private String image = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sign_person, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        int rvWidth = screenWidth - (int) dimension * 2;
//        ViewGroup.LayoutParams layoutParams = mRecyclerView.getLayoutParams();
//        layoutParams.width = screenWidth;
//        layoutParams.height = rvHeight;
//        mRecyclerView.setLayoutParams(layoutParams);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        adapter = new SignSeletPictureAdapter(getActivity(), null);
        mRecyclerView.addItemDecoration(new SpaceItemDecoration((int) getActivity().getResources().getDimension(R.dimen.x18)));
        mRecyclerView.setAdapter(adapter);
        initData();

        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnRecyclerViewItemClickListener<ImageItem>() {
            @Override
            public void onItemClick(View view, List<ImageItem> data, int position) {
                LogUtil.e("===onItemClick===");
                if (position == 0) {
                    LogUtil.e("===拍照===");
                    ((SignActivity) getActivity()).getPicture(((SignActivity) getActivity()).TAKE_PHOTO);
                } else if (position == data.size() - 1) {
                    ((SignActivity) getActivity()).getPicture(((SignActivity) getActivity()).IMAGE_PICKER);

                } else {
                    image = data.get(position).path;
                    for (int i = 0; i < data.size(); i++) {
                        if (data.get(i).select) {
                            data.get(i).select = false;
                            adapter.notifyItemChanged(i);
                        } else if (position == i) {
                            data.get(i).select = true;
                            adapter.notifyItemChanged(position);
                        }
                    }

                }
            }
        });
    }

    public void initData() {
        if (adapter.data.size() < 1) {
            pagerNum = 0;
            refreshData();
        }
    }

    public void refreshData() {
        int endP = (pagerNum + 1) * pagerSize;
        int startP = pagerNum * pagerSize;
        if (((SignActivity) getActivity()).allImages.size() == 1) {
            return;
        }
        if (((SignActivity) getActivity()).allImages.size() > endP) {
            List<ImageItem> imageItems = ((SignActivity) getActivity()).allImages.subList(startP, endP);
            adapter.append(imageItems);
        } else {
            List<ImageItem> imageItems = ((SignActivity) getActivity()).allImages.subList(startP, ((SignActivity) getActivity()).allImages.size() - 1);
            if (((SignActivity) getActivity()).allImages.size() == 1) {
                imageItems.add(new ImageItem());
            }
            adapter.append(imageItems);
        }
        pagerNum++;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.tv_complete)
    public void onViewClicked() {
        if (!TextUtils.isEmpty(image)){
            ((SignActivity) getActivity()).setImage(image);
        }
    }

}
